package com.zzh.ideas.web.framework.handler;

import com.zzh.ideas.web.model.TestReuest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * description: <br>
 * createTime: 2019/8/1610:30 <br>
 *
 * @author zzh
 */
@Slf4j
@ControllerAdvice
public class RequestValueHandler implements RequestBodyAdvice {

    public RequestValueHandler() {
        System.out.println("lakjsdlf");
    }


    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        // 符合什么条件的才拦截
        return true;
    }
    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
        return httpInputMessage;
    }
    @Override
    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        // TODO 数据合法性检查
//        String result = ValidationUtil.validateOrder(o);
//        if(StringUtils.isNotBlank(result)) {
//            log.warn("请求入参(数据校验失败：" + result + ")：{}", JSON.toJSONString(o));
//            throw new DataException(o, result);
//        }
        // TODO 请求参数日志
//        if (log.isInfoEnabled()) {
//            log.info("请求入参：{}", JSON.toJSONString(o));
//        }
        if(o instanceof TestReuest) {
            TestReuest testReuest = (TestReuest) o;
//            testReuest.getReqdata().put("userid", "12345");
        }
        return o;
    }


    @Override
    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
        return null;
    }
}
